<?php
namespace app\redbag\controller;
use app\AdminRest;
use app\card\model\DefaultSetting;
use think\App;
use app\redbag\model\AdminActive as Model;
use app\redbag\model\AdminCompany;
//use app\passenger\model\Coupon;

use think\facade\Cache;
use think\facade\Db;

class AdminActive extends AdminRest
{

    protected $model;

    protected $company_model;

    //protected $coupon_model;


    public function __construct(App $app) {
        parent::__construct($app);
        $this->model         = new Model();

        $this->company_model = new AdminCompany();

        //$this->coupon_model  = new Coupon();
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:30
     * @return void
     * descrption:列表
     */
    public function activeList(){
       $input = $this->_input;
        //uniacid
       $dis[] = ['uniacid','=',$this->_uniacid];
        //查询没有被删除对
       $dis[] = ['status','>',-1];
       //查询
       if(!empty($input['title'])){
           $dis[] = ['title','like',"%".$input['title']."%"];
       }
       //发布者
       if(!empty($input['create_user'])){

           $icon = $input['create_user']==2?'=':'<>';

           $dis[] = ['create_user',$icon,0];
       }
        //查询
       $data  = $this->model->activeList($dis,$input['limit']);

        return $this->success($data);
    }



    /**
     * @author chenniang
     * @DataTime: 2020-03-18 12:26
     * @功能说明:默认配置
     */
    public function defaultSetting(){

        $input   = $this->_param;

        $default = new DefaultSetting();

        if(count($input)>1&&isset($input['redbag_staff_switch'])){

            //新的配置
            $data = $default->settingUpdate(['uniacid' => $this->_uniacid],['redbag_staff_switch'=>$input['redbag_staff_switch']]);

        }else{
            //新的配置
            $data = $default->settingInfo(['uniacid' => $this->_uniacid]);
        }

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 09:20
     * @功能说明:活动详情
     */
    public function activeInfo(){

        $input = $this->_input;

        $data  = $this->model->activeInfo(['id'=>$input['id']]);

        $dis = [
            //uniacid
            'a.uniacid' => $this->_uniacid,
            //活动id
            'a.active_id'=> $input['id']
        ];
        //公司
        $data['company'] = $this->company_model->getId($dis);
        //员工
        $data['staff']   = $this->company_model->getStaff($dis);

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-09 18:48
     * @功能说明:添加活动
     */
    public function activeAdd(){

        $input = $this->_input;
        //转换格式
        $input = $this->changeData($input);
        //添加
        $res   = $this->model->activeAdd($input);

        return $this->success($res);
    }

    /**
     * @author chenniang
     * @DataTime: 2020-03-09 18:49
     * @功能说明:转换数据
     */
    public function changeData($input){
        //uniacid
        $input['uniacid'] = $this->_uniacid;
        //公司
        if(!empty($input['company'])){

            $input['company'] =  $this->company_model->changeData($input['company']);
        }

        return $input;
    }





}
